import { commonPost } from '@api/data'

import { Printerconf} from '@api/tools/rprinter.js'
import moment from "moment";

var  regReciptId="";
var  queueNo="";
var offset=400;
// 倒计时
export const countDown = {
  methods: {
    resetCountDown() { // 重置倒计时
      const totalTime = 60
      this.$store.dispatch("timer/updateTimeCount", totalTime)
    }
  }
}
// 路由
export const route = {
  methods: {
    backToPreSatgeEnd() {  // 返回流程结束页
      const endName = this.$store.state.route.preStageEnd
      // this.$router.push({ name: endName })
      this.$router.push({ name: "home" })
    }
  }
}
function LogError(str) {
  console.log(`%c ${str}`, 'color:red');
}
// 预约
export const appointent = {
  methods: {
    sendAppointent() { // 预约取号请求
      let data = {}
      let appointmentInfo=this.$store.state.appointment.appointmentInfo;
      let userinfo=this.$store.state.user.userinfo;
      let payinfo=this.$store.state.payrecord.payinfo;
      var miinfo=this.$store.state.user.miinfo;
      data.url = "/api/PreRegConfirm"
      data.payload = {
        "termId": this.$store.state.terminfo.terminfo.termId,
        "hisOperNo": this.$store.state.terminfo.terminfo.hisOperNo,
        "regDate":appointmentInfo.regDate,
        "type": appointmentInfo.type,
        "regReciptId":appointmentInfo.regReciptId,
        "regNo":appointmentInfo.regNo,
        "scheduleId":appointmentInfo.scheduleId,
        "deptId": appointmentInfo.deptId,
        "deptName": appointmentInfo.deptName,
        "doctorId": appointmentInfo.doctorId,
        "doctorName": appointmentInfo.doctorName,
        "timeValue": appointmentInfo.timeValue,
        "interval":appointmentInfo.interval,
        "cardNo":userinfo.cardNo,
        "cardType":userinfo.cardType,
        "patientId":userinfo.patientId,
        "phone":appointmentInfo.phone,
        "payWay":payinfo.payWay,
        "fee":appointmentInfo.fee,
        "userpay":this.$store.state.user.userpay,
        "sourceID":payinfo.barCode,
        "outPayTradno":payinfo.outPayTradno,
        "rcptNo":payinfo.rcptNo,
        "transNo":payinfo.transNo,
      }
      var isYbPay=this.$store.state.user.isYbPay
      var payWay=payinfo.payWay;
      console.log("是否医保="+isYbPay+"支付方式="+payWay);
      if(isYbPay){
        let orginfo = this.$store.state.terminfo.orginfo;
        var regReciptId=this.$store.state.medicalInsurance.regmsghis.regReciptId;
        var insurePayStr;
        var miTransNo;
        if(orginfo.cmiid==='104'){//延吉挂号登记后即医保扣款结束
          insurePayStr=this.$store.state.medicalInsurance.regmsgdll;
          insurePayStr=this.$store.state.medicalInsurance.regmsgdll;
          var  medInsRegistMsg=this.$store.state.medicalInsurance.regmsghis.medInsRegistMsg;
          miTransNo = medInsRegistMsg.split("^")[4];
          console.log("miTransNo="+miTransNo);
          data.payload["miArea"]=miinfo[13];//统筹区域
          data.payload["miOtherArea"]=miinfo[40];//异地医保标志
          data.payload["miTransNo"]=miTransNo;//医保挂号登记请求流水号
        }
       else{
        insurePayStr=this.$store.state.medicalInsurance.confirmhis.hisRcptNo+'|'+this.$store.state.medicalInsurance.confirmdll.split("^")[2];
       }
        console.log("医保挂号串："+insurePayStr);
        data.payload["regReciptId"]=regReciptId;
        data.payload["insurePayStr"]=insurePayStr;
      }
      commonPost(data).then(res => {
        regReciptId=res.data[0].regReciptId;
        queueNo=res.data[0].queueNo;
        var sceneid = this.$store.state.terminfo.seceninfo.sceneid;
           if(sceneid==='sstbg'){
            offset=800;
        }
        // if(sceneid==='sstbg'){
        //     offset=800;
        //     this.Printerbg();
        // }
        // else{
        //   this.Print();
        // }
        this.Printgh(res);
        this.$notify({
          dangerouslyUseHTMLString: true,
          offset: offset,
          type:'success',
          duration:8000,
          message: '<h1 style="color:green;font-size:18px">挂号成功！'+'</h1>'+
          '<h1>科室名称：' + this.$store.state.appointment.appointmentInfo.deptName + '</h1>'+
          '<h1>医生名称：' + this.$store.state.appointment.appointmentInfo.doctorName + '</h1>'+
          '<h1>挂号时间：' + this.$store.state.appointment.appointmentInfo.regDate + ' ' + this.$store.state.appointment.appointmentInfo.timeValue + '</h1>'+
          '<h1 style="color:red">此凭条可用于开具报销发票，遗失不补！'+'</h1>'
          });
      }).catch(e=>{
        if(isYbPay){
          LogError("his失败，医保结算撤销");
          this.RetMi();
        }
      });
    },
    sendRegConfirm() {// 挂号请求
      let data = {}
      let appointmentInfo=this.$store.state.appointment.appointmentInfo;
      let userinfo=this.$store.state.user.userinfo;
      let payinfo=this.$store.state.payrecord.payinfo;
      var miinfo=this.$store.state.user.miinfo;
      data.url = "/api/RegConfirm"
      data.payload = {
        "termId": this.$store.state.terminfo.terminfo.termId,
        "hisOperNo": this.$store.state.terminfo.terminfo.hisOperNo,
        "regDate":appointmentInfo.regDate,
        "type": appointmentInfo.type,
        "regNo":appointmentInfo.regNo,
        "scheduleId":appointmentInfo.scheduleId,
        "deptId": appointmentInfo.deptId,
        "deptName": appointmentInfo.deptName,
        "doctorId": appointmentInfo.doctorId,
        "doctorName": appointmentInfo.doctorName,
        "timeValue": appointmentInfo.timeValue,
        "interval":appointmentInfo.interval,
        "cardNo":userinfo.cardNo,
        "cardType":userinfo.cardType,
        "patientId":userinfo.patientId,
        "phone":appointmentInfo.phone,
        "payWay":payinfo.payWay,
        "userpay":this.$store.state.user.userpay,
        "fee":appointmentInfo.fee,
        "regFee":appointmentInfo.regFee,
        "otherFee":appointmentInfo.otherFee,
        "sourceID":payinfo.barCode,
        "outPayTradno":payinfo.outPayTradno,
        "rcptNo":payinfo.rcptNo,
        "transNo":payinfo.transNo,
      }
      var isYbPay=this.$store.state.user.isYbPay
      var payWay=payinfo.payWay;
      console.log("是否医保="+isYbPay+"支付方式="+payWay);
      if(isYbPay){
        let orginfo = this.$store.state.terminfo.orginfo;
        var regReciptId=this.$store.state.medicalInsurance.regmsghis.regReciptId;
        var insurePayStr;
        var miTransNo;
        if(orginfo.cmiid==='104'){//延吉挂号登记后即医保扣款结束
          insurePayStr=this.$store.state.medicalInsurance.regmsgdll;
          var  medInsRegistMsg=this.$store.state.medicalInsurance.regmsghis.medInsRegistMsg;
          miTransNo = medInsRegistMsg.split("^")[4];
          console.log("miTransNo="+miTransNo);
          data.payload["miArea"]=miinfo[13];//统筹区域
          data.payload["miOtherArea"]=miinfo[40];//异地医保标志
          data.payload["miTransNo"]=miTransNo;//医保挂号登记请求流水号
        }
       else{
          insurePayStr=this.$store.state.medicalInsurance.confirmhis.hisRcptNo+'|'+this.$store.state.medicalInsurance.confirmdll.split("^")[2];
       }
        console.log("医保挂号串："+insurePayStr);
        data.payload["regReciptId"]=regReciptId;
        data.payload["insurePayStr"]=insurePayStr;
      }
      commonPost(data).then(res => {

        var sceneid = this.$store.state.terminfo.seceninfo.sceneid;
          if(sceneid==='sstbg'){
            offset=800;
        }

        this.Printgh(res);
        this.$notify({
            dangerouslyUseHTMLString: true,
            offset: offset,
            type:'success',
            duration:8000,
            message: '<h1 style="color:green;font-size:18px">挂号成功！'+'</h1>'+
            '<h1 >科室名称：' + this.$store.state.appointment.appointmentInfo.deptName + '</h1>'+
            '<h1>医生名称：' + this.$store.state.appointment.appointmentInfo.doctorName + '</h1>'+
            '<h1>挂号时间：' + this.$store.state.appointment.appointmentInfo.regDate + ' ' + this.$store.state.appointment.appointmentInfo.timeValue + '</h1>'+
            '<h1 style="color:red">此凭条可用于开具报销发票，遗失不补！'+'</h1>'
          });
      }).catch(e=>{
        if(isYbPay){
        LogError("his失败，医保结算撤销");
         this.RetMi();
        }
      });
    },
    async Printgh(res) {
      const pconf=new Printerconf();
      var receiptPrinterModel=this.$store.state.terminfo.terminfo.receiptPrinterModel;
      let printdata={};
      printdata.queueNo=res.data[0].queueNo;
      printdata.regReciptId=res.data[0].regReciptId;
      printdata.adddress=res.data[0].adddress;
      console.log("开始打印="+receiptPrinterModel);
      console.log("开始打印 就诊位置="+printdata.adddress);
      await pconf.mzghprint(receiptPrinterModel,this,printdata);
      // this.$router.push({ name: "home"});
    },
    async RetMi(){
      let orginfo = this.$store.state.terminfo.orginfo;
      console.log("结算撤销="+this.$store.state.medicalInsurance.confirmhis.medInsRetBillConfirmMsg+
      ";费用撤销="+this.$store.state.medicalInsurance.upfeehis.medInsRetUpFeeMsg+
      "挂号登记撤销;="+this.$store.state.medicalInsurance.regmsghis.medInsRetRegistMsg);
      if(orginfo.cmiid==='104'){//延吉挂号登记后即医保扣款结束
        //挂号登记撤销
        this.YB.Trade(1, this.$store.state.medicalInsurance.regmsghis.medInsRetRegistMsg);
      }
      else{
      //结算撤销
      await this.YB.Trade(1,  this.$store.state.medicalInsurance.confirmhis.medInsRetBillConfirmMsg);
      //费用撤销
      await this.YB.Trade(1, this.$store.state.medicalInsurance.upfeehis.medInsRetUpFeeMsg);
      //
      await this.YB.Trade(1, this.$store.state.medicalInsurance.regmsghis.medInsRetRegistMsg);
      }

    }
  },
  computed:{
    appointmentInfo() {
      return this.$store.state.appointment.appointmentInfo
    },
  }
}
// 缴费
export const recipel = {
  methods: {
    sendRecipel() {//缴费请求
      console.log(this.$store.state.recipel.invoiceNo);
     let records=this.$store.state.recipel.records;
     let billinfo=this.$store.state.recipel.billinfo
     let userinfo=this.$store.state.user.userinfo;
     let payinfo=this.$store.state.payrecord.payinfo;
     var miinfo=this.$store.state.user.miinfo;
     let temp='';
     for(let i of billinfo.Grouplist){
        temp +=`'${i.groupId}',`;
     }
     temp=temp.substring(0,temp.length-1)
     let prescIds="("+temp+")";

    let data = {}
      data.url = "/api/RecipelConfirm"
      data.payload = {
        cardType:"0",
        invoiceNo:this.$store.state.recipel.invoiceNo,
        cardNo:userinfo.cardNo,
        patientId:userinfo.patientId,
        hisInsuInfo:"",
        regReciptId: records.ybAdmId,
        prescIds:prescIds,
        orderNo:"",
        admRowId:records.admRowId,
        sourceID:payinfo.barCode,
        userpay:this.$store.state.user.userpay,
        fee:records.fee,
        payWay:payinfo.payWay,
        outPayTradno:payinfo.outPayTradno,
        payTrandNo:payinfo.transNo,
        rcptNo:payinfo.rcptNo,
        chargeType:userinfo.chargeType,
        hisOperNo:this.$store.state.terminfo.terminfo.hisOperNo,
        termId:this.$store.state.terminfo.terminfo.termId,
      }
      var isYbPay=this.$store.state.user.isYbPay
      var payWay=payinfo.payWay;
      console.log("是否医保="+isYbPay+"支付方式="+payWay);
      if(isYbPay){
        let orginfo = this.$store.state.terminfo.orginfo;
        var insurePayStr;
        var miTransNo;
        if(orginfo.cmiid==='104'){//延吉挂号登记后即医保扣款结束
          insurePayStr=this.$store.state.medicalInsurance.confirmdll;
          var  medInsRegistMsg=this.$store.state.medicalInsurance.confirmhis.medInsBillConfirmMsg;
           miTransNo = medInsRegistMsg.split("^")[4];
          console.log("miTransNo="+miTransNo);
          data.payload["miArea"]=miinfo[13];//统筹区域
          data.payload["miOtherArea"]=miinfo[40];//异地医保标志
          data.payload["miTransNo"]=miTransNo;//医保挂号登记请求流水号
        }
       else{
          insurePayStr=this.$store.state.medicalInsurance.confirmhis.hisRcptNo+'|'+this.$store.state.medicalInsurance.confirmdll.split("^")[2];
       }
        console.log("医保挂号串："+insurePayStr);
        data.payload["insurePayStr"]=insurePayStr;
      }
      commonPost(data).then(res => {
        var sceneid = this.$store.state.terminfo.seceninfo.sceneid;
         if(sceneid==='sstbg'){
              offset=800;
        }
        this.Printmzjs(res.data);
        this.$notify({
          dangerouslyUseHTMLString: true,
          offset: offset,
          type:'success',
          duration:8000,
          message: '<h1 style="color:green;font-size:18px">缴费成功！'+'</h1>'+
          '<h1>科室名称：' + records.deptName + '</h1>'+
          '<h1>医生名称：' + records.doctorName + '</h1>'+
          '<h1>就诊时间：' + records.admDate +  '</h1>'+
          '<h1>总费用：' + records.fee +  '</h1>'+
          '<h1 style="color:red">此凭条可用于开具报销发票，遗失不补！'+'</h1>',
          });
      }).catch(e=>{
        if(isYbPay){
          LogError("医保结算失败，结算撤销");
          //结算撤销
          this.YB.Trade(1,  this.$store.state.medicalInsurance.confirmhis.medInsRetBillConfirmMsg);
          //费用撤销
          this.YB.Trade(1, this.$store.state.medicalInsurance.upfeehis.medInsRetUpFeeMsg);
        }
      });
    },

    async Printmzjs(data) {
      const pconf=new Printerconf();
      var receiptPrinterModel=this.$store.state.terminfo.terminfo.receiptPrinterModel;
        console.log("开始打印="+receiptPrinterModel);
        await pconf.mzjsprint(receiptPrinterModel,this,data);
        this.$router.push({ name: "home"});

    },
  }
}
// 住院充值
export const inptRechargeReq = {
  methods: {
    sendInptRechargeReq() {//住院充值
      console.log("住院充值");
     let userinfo=this.$store.state.user.userinfo;
     let chargefee=this.$store.state.user.userpay;
     let payinfo=this.$store.state.payrecord.payinfo;

    let data = {}
      data.url = "/api/InptRecharge"
      data.payload = {
        patientId:userinfo.patientId,
        idNo:userinfo.idNo,
        name:userinfo.name,
        payTime:userinfo.patientId,
        sourceID:payinfo.barCode,
        chargeFee:chargefee,
        payWay:payinfo.payWay,
        outPayTradno:payinfo.outPayTradno,
        payTrandNo:payinfo.transNo,
        rcptNo:payinfo.rcptNo,
        hisOperNo:this.$store.state.terminfo.terminfo.hisOperNo,
        termId:this.$store.state.terminfo.terminfo.termId,
      }
      commonPost(data).then(res => {
        var sceneid = this.$store.state.terminfo.seceninfo.sceneid;
        if(sceneid==='sstbg'){
          offset=800;
      }
      this.Printzycz(res.data);
        var printime = moment().format("YYYY-MM-DD HH:mm:SS");
        this.$notify({
          dangerouslyUseHTMLString: true,
          offset: 400,
          type:'success',
          duration:8000,
          message: '<h1 style="color:green;font-size:18px">住院充值成功！'+'</h1>'+
          '<h1>姓名：' + userinfo.name + '</h1>'+
          '<h1>充值金额：' + chargefee + '</h1>'+
          '<h1>充值时间：' +printime+  '</h1>'+
          '<h1 style="color:red">此凭条可用于开具报销发票，遗失不补！'+'</h1>',
          });
      })
    },
    async Printzycz(data) {
      const pconf=new Printerconf();
      var receiptPrinterModel=this.$store.state.terminfo.terminfo.receiptPrinterModel;
        console.log("开始打印="+receiptPrinterModel);
        await pconf.zyczprint(receiptPrinterModel,this,data);
        this.$router.push({ name: "home"});
    },
  },

}

